fix(config): atomic write for .env to prevent API key loss on crash#954
Merged
fix(config): atomic write for .env to prevent API key loss on crash#954
Conversation
save_env_value() used bare open('w') which truncates .env immediately.
A crash or OOM kill between truncation and completed write silently
wipes every credential in the file.
Write now goes to a temp file first, then os.replace() swaps it
atomically. Either the old .env exists or the new one does — never
a truncated half-write. Same pattern used in cron/jobs.py.
Cherry-picked from PR #842 by alireza78a, rebased onto current main
with conflict resolution (_secure_file refactor).
Co-authored-by: alireza78a <alireza78a@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cherry-picked from PR #842 by @alireza78a, rebased onto current main with conflict resolution.
save_env_value()used bareopen('w')which truncates.envimmediately. A crash or OOM kill between truncation and the completed write silently wipes every credential in the file.Fix
Write now goes to a temp file in the same directory first (
tempfile.mkstemp), thenos.replace()swaps it atomically. Either the old.envexists or the new one does — never a truncated half-write. Same pattern already used insave_jobs()incron/jobs.py.Conflict resolution
The PR was 165 commits behind main. The only conflict was that main had refactored the inline
os.chmodinto_secure_file(env_path). Resolved by keeping the atomic write pattern AND the_secure_file()call.Tests
201 hermes_cli tests pass.
Closes #842
Co-authored-by: alireza78a alireza78a@users.noreply.github.com